/**
 * @file zediskapp.cpp
 * @author zeppyrz (zeppyrz@gmail.com)
 * @brief ZeDisk GUI Class implementation
 * @version 0.1
 * @date 2025-11-03
 *
 * @copyright Copyright (c) 2025
 *
 */

#include "zediskapp.h"

#ifdef _WIN32
// clang-format off
#  include <winsock2.h>
#  include <windows.h>
// clang-format on
#endif

#include <QFileDialog>
#include <QMessageBox>
#include <iostream>

#include "ThreadPool.h"
#include "ZeDiskClient.h"
#include "ui_zediskapp.h"

#ifndef _WIN32
#  include <signal.h>
#endif

namespace ZeDiskGUI {

using namespace ZeDisk;

ZeDiskApp::ZeDiskApp(QWidget *parent) : QWidget(parent), ui(new Ui::ZeDiskApp) {
  ui->setupUi(this);
#ifdef _WIN32
  WSADATA wsaData;
  WSAStartup(MAKEWORD(2, 2), &wsaData);
#else
  if (signal(SIGPIPE, SIG_IGN) == SIG_ERR) return;
#endif
  (void)ZeDiskClient::GetInstance();
  QObject::connect(&ZeDiskClient::GetInstance(), &ZeDiskClient::DirSig, this,
                   &ZeDiskApp::UpdateDir);
  QObject::connect(&ZeDiskClient::GetInstance(),
                   &ZeDiskClient::UploadCompleteSig, this, &ZeDiskApp::Refresh);
  QObject::connect(&ZeDiskClient::GetInstance(),
                   &ZeDiskClient::DownloadCompleteSig, this,
                   &ZeDiskApp::DownloadComplete);
  Refresh();
}

ZeDiskApp::~ZeDiskApp() { delete ui; }

void ZeDiskApp::UpdateServerInfo() {
  std::string ip = ui->ipEdit->text().toStdString();
  std::string root = ui->pathEdit->text().toStdString();
  int port = ui->portSpinBox->value();

  ZeDiskClient::GetInstance().root() = root.empty() ? "./" : root;
  ZeDiskClient::GetInstance().server_ip() = ip;
  ZeDiskClient::GetInstance().server_port() = port;
}

void ZeDiskApp::Refresh() {
  // QMessageBox::information(this, tr("Information"), tr("Refresh"));
  UpdateServerInfo();
  ZeDiskClient::GetInstance().GetDir();

  // 设置回调
}

void ZeDiskApp::UpdateDir(std::string dirs) {
  QString str = dirs.c_str();
  str = str.trimmed();
  if (str.isEmpty()) return;
  auto files = str.split(";");
  ui->filelistWidget->setRowCount(0);
  for (auto &file : files) {
    auto file_info = file.split(',');
    if (file_info.size() != 2) continue;
    ui->filelistWidget->insertRow(0);
    ui->filelistWidget->setItem(0, 0, new QTableWidgetItem(file_info[0]));
    ui->filelistWidget->setItem(
        0, 1, new QTableWidgetItem(tr("%1Bytes").arg(file_info[1])));
  }
}

void ZeDiskApp::Upload() {
  const auto filename = QFileDialog::getOpenFileName(
      this, QString::fromLocal8Bit("请选择上传文件"));

  if (filename.isEmpty()) {
    return;
  }

  UpdateServerInfo();
  ZeDiskClient::GetInstance().Upload(filename.toStdString());
}

void ZeDiskApp::Download() {
  // 选择下载的文件 和 保存路径
  auto row = ui->filelistWidget->currentRow();
  if (row < 0) {
    QMessageBox::information(this, "",
                             QString::fromLocal8Bit("请选择下载文件"));
    return;
  }

  // 获取下载文件名
  std::string file_name =
      "/" + ui->filelistWidget->item(row, 0)->text().toStdString();
  // 获取保存路径
  auto directory = QFileDialog::getExistingDirectory(
      this, QString::fromLocal8Bit("请选择下载路径"));
  if (directory.isEmpty()) return;
  std::string local_path = directory.toStdString() + file_name;
  ZeDiskClient::GetInstance().Download(file_name, local_path);
}

void ZeDiskApp::DownloadComplete() {
  QMessageBox::information(this, "", "Download Complete");
}

}  // namespace ZeDiskGUI
